package com.zy.DingDongServer.service;

import com.zy.DingDongServer.dao.FriendDao;
import com.zy.DingDongServer.dao.UserDao;
import com.zy.DingDongServer.entity.Friend;
import com.zy.DingDongServer.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;

@Service
public class FriendService {

    @Autowired
    private FriendDao friendDao;
    @Autowired
    private UserDao userDao;
    
    //根据ownerId获取好友
    public ArrayList<Friend> getFriendsByUser(User user){
        try {
            int ownerId = user.getId();
            return friendDao.getByOwnerId(ownerId);
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

    //获取新的好友请求
    public ArrayList<Friend> getNewFriendRequestByUser(User user){
        try {
            int ownerId = user.getId();
            return friendDao.getNewRequestByOwnerId(ownerId);
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }
    //添加好友请求
    public String sendFriendRequest(int userId,int friendId){
        try {
            Friend friend = friendDao.checkFriendRequest(friendId,userId);
            if (friend != null){
                return "该好友已经发送过好友请求";
            }
            Friend newFriend = new Friend(friendId,userId,Friend.REJECT,Friend.IS_NEW);
            //此时朋友会查到这条记录
            friendDao.insert(newFriend);
            return "success";
        }catch (Exception e){
            e.printStackTrace();
            return "好友请求系统错误";
        }
    }
    //接受好友请求
    public User accept(int ownerId,int friendId){
        try {
            //先检查对方是否发送了好友通知
            Friend friend = friendDao.checkFriendRequest(ownerId,friendId);
            //该好友没有发送添加好友请求通知
            if (friend == null){
                return null;
            }
            //接受请求并改变状态
            friendDao.acceptFriend(friend.getId());
            friendDao.changeNewFriendStatus(friend.getId());
            User newFriend = userDao.getById(friendId);
            return newFriend;
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }
    //拒绝好友请求
    public String reject(int userId,int friendId){
        try {
            //先检查对方是否发送了好友请求
            Friend friend = friendDao.checkFriendRequest(userId,friendId);
            if (friend == null){
                return "该好友并未发送好友请求";
            }
            //删除这条好友请求
            friendDao.deleteFriend(friend.getId());
            return "success";
        }catch (Exception e){
            e.printStackTrace();
            return "系统错误";
        }
    }
    //删除好友
    public String delete(int userId,int friendId){
        try {
            Friend friend1 = friendDao.checkIsFriend(userId,friendId);
            Friend friend2 = friendDao.checkIsFriend(friendId,userId);
            if (friend1 == null && friend2 == null){
                return "对方不是您的好友";
            }
            if (friend1 != null)
                friendDao.deleteFriend(friend1.getId());
            else
                friendDao.deleteFriend(friend2.getId());
            return "success";
        }catch (Exception e){
            e.printStackTrace();
            return "系统错误";
        }
    }
    //获取新好友的数据，is_accept =1 ,is_new = 2
    public ArrayList<User> getNewFriends(int ownerId){
        try {
            //获取新朋友集合
            ArrayList<Friend> friends = friendDao.getNewFriendsByOwnerId(ownerId);
            if (friends == null || friends.size() == 0){
                return null;
            }
            ArrayList<User> newFriends = new ArrayList<>();
            for (Friend friend:friends){
                //改变状态，设置为不是新朋友
                friendDao.changeNewFriendStatus(friend.getId());
                User newFriend = null;
                //ownerId既可以是OwnerId也可以是friendId
                if (friend.getFriendId() != ownerId)
                    newFriend = userDao.getById(friend.getFriendId());
                else
                    newFriend = userDao.getById(friend.getOwnerId());
                newFriends.add(newFriend);
            }
            //返回新朋友的User对象
            return newFriends;
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }
    //
    public void changeNewFriendStatus(int friendId,int ownerId){
        HashMap<String,Integer> params = new HashMap<>();
        params.put("friendId",friendId);
        params.put("ownerId",ownerId);
        friendDao.changeNewFriendStatus2(params);
    }

    //检查是否是好友
    public String checkIsFriend(int userId,int friendId){
        try {
            Friend friend1 = friendDao.checkIsFriend(userId,friendId);
            Friend friend2 = friendDao.checkIsFriend(friendId,userId);
            if (friend1 == null && friend2 == null){
                return "对方不是您的好友";
            }
            return "success";
        }catch (Exception e){
            e.printStackTrace();
            return "系统错误";
        }
    }
}
